package cn.manjuu.touhou.engine;

import java.util.List;

import android.content.Context;

import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.db.dao.BaseInfoDao;
import cn.manjuu.touhou.domain.BaseInfo;
import cn.manjuu.touhou.util.Looger;

/**
 * 处理BaseInfo的业务类
 * 
 * @author 宋疆疆
 * 
 */
public class BaseInfoEngine {

	private Context mContext;
	private BaseInfo mBaseInfo;
	private static BaseInfoEngine mInstance;
	private BaseInfoDao mBaseInfoDao;

	public static BaseInfoEngine getInstance() {
		if (null == mInstance) {
			mInstance = new BaseInfoEngine();
		}
		return mInstance;
	}

	private BaseInfoEngine() {
		super();
	}

	public void setContext(Context mContext) {
		this.mContext = mContext;
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		mBaseInfoDao = new BaseInfoDao(mContext);
	}

	public BaseInfo getBaseInfo() {
		return mBaseInfo;
	}

	/**
	 * 初始化玩家信息
	 */
	public void initPlayer() {
		List<BaseInfo> queryAll = mBaseInfoDao.queryAll();
		if (0 == queryAll.size()) {
			mBaseInfo = new BaseInfo("馒头儿", 1000);
			long insert = mBaseInfoDao.insert(mBaseInfo);
			mBaseInfo.set_id(insert);
			Looger.i(APP.SplahActivity, "baseInfo id: " + insert);
		} else {
			mBaseInfo = queryAll.get(0);
		}
	}

	/**
	 * 减少玩家的钱
	 * 
	 * @param i
	 */
	public int reduceMoney(long i) {
		// TODO Auto-generated method stub
		mBaseInfo.setMoney(mBaseInfo.getMoney() - i);
		Looger.i(APP.BaseInfoEngine, "mBaseInfo: " + mBaseInfo);
		int update = mBaseInfoDao.update(mBaseInfo);
		return update;
	}

	/**
	 * 增加玩家的钱
	 * 
	 * @param i
	 */
	public int increaseMoney(long i) {
		// TODO Auto-generated method stub
		mBaseInfo.setMoney(mBaseInfo.getMoney() + i);
		int update = mBaseInfoDao.update(BaseInfoEngine.getInstance().getBaseInfo());
		return update;
	}
}
